<?php
declare (strict_types=1);

namespace app\admin\validate;

use think\Validate;

class WebsiteConfig extends Validate
{
    protected $rule = [
        'name'         => 'require',
        'config_code'  => 'require|checkExistCode',
        'config_group' => 'require',
        'status'       => ['require', 'in' => '1,2'],
    ];

    protected $message = [
        'name.require'         => '请输入配置项名称',
        'config_code.require'  => '请输入配置项标识',
        'config_group.require' => '请选择配置项分组',
        'status'               => '请选择正确的状态',
    ];

    protected $scene = [
        'add'  => ['name', 'config_code', 'status'],
        'edit' => ['name', 'status']
    ];

    public function checkExistCode($value, $rule, $data = [])
    {
        $websiteConfigModel = new \app\common\model\WebsiteConfig([], $data['website_code']);
        $websiteConfig      = $websiteConfigModel->findWebsiteConfigByCode($value);
        if (!empty($websiteConfig)) {
            return '字段标识已存在';
        }

        return true;
    }
}
